import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_FRM_PALLET_STASH_OPS_DATA,
  SAGA_FRM_PALLET_LOAD_ITEM_DETAILS,
  SAGA_FRM_PALLET_LOAD_BARCODE_DETAILS,
  SAGA_FRM_PALLET_LOAD_OPS_FORM,
  SAGA_FRM_PALLET_DEL_BARCODE,
  SAGA_FRM_PALLET_FINISH_OPS,
  SAGA_FRM_PALLET_INIT_LOAD_OPS_FORM,
  SAGA_FRM_PALLET_LOAD_ORDER_LINE,
  SAGA_FRM_PALLET_PUSH_2_SERVER
} from "./saga-types";
import {
  SET_FRM_PALLET_ITEM_DETAILS,
  SET_FRM_PALLET_BARCODE_DETAILS,
  SET_FRM_PALLET_OPS_FORM,
  DEL_FRM_PALLET_BARCODE,
  SET_FRM_PALLET_ORDER_LINE
} from "./action-types";
import { SET_USER_PROFILE_DEFAULT_SCENARIO } from "../user-profile/action-types";

function* stashFrmPalletOpsDataSaga() {
  yield takeEvery(SAGA_FRM_PALLET_STASH_OPS_DATA, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletOpsHandler,
            action: "create",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_OPS_FORM],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* loadFrmPalletItemDetailsSaga() {
  yield takeEvery(SAGA_FRM_PALLET_LOAD_ITEM_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletDetailsHandler,
            action: "loadItemDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_ITEM_DETAILS],
        },
      ],
    });
  });
}

function* loadFrmPalletBarcodeDetailsSaga() {
  yield takeEvery(SAGA_FRM_PALLET_LOAD_BARCODE_DETAILS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletDetailsHandler,
            action: "loadBarcodeDetails",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_BARCODE_DETAILS],
        },
      ],
    });
  });
}

function* loadFrmPalletOpsFormSaga() {
  yield takeEvery(SAGA_FRM_PALLET_LOAD_OPS_FORM, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_OPS_FORM],
        },
      ],
    });
  });
}

function* delFrmPalletBarcodeSaga() {
  yield takeEvery(SAGA_FRM_PALLET_DEL_BARCODE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletOpsHandler,
            action: "delBarcode",
            needNonce: false,
            params: action.payload,
          },
          respActions: [DEL_FRM_PALLET_BARCODE],
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* finishFrmPalletOpsSaga() {
  yield takeEvery(SAGA_FRM_PALLET_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* initLoadFrmPalletOpsForm() {
  yield takeEvery(SAGA_FRM_PALLET_INIT_LOAD_OPS_FORM, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.UserProfileHandler,
            action: "loadDefaultScenario",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_USER_PROFILE_DEFAULT_SCENARIO],
        },
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_OPS_FORM],
          callback: action.payload.callback
        },
      ],
    });
  });
}

function* loadFrmPalletOrderLine() {
  yield takeEvery(SAGA_FRM_PALLET_LOAD_ORDER_LINE, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletHandler,
            action: "loadOrderLine",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_FRM_PALLET_ORDER_LINE]
        },
      ]
    });
  });
}

function* pushFrmPallet2ServerSaga() {
  yield takeEvery(SAGA_FRM_PALLET_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.FrmPalletHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback
        },
      ],
    });
  });
}

export default [
  stashFrmPalletOpsDataSaga(),
  loadFrmPalletItemDetailsSaga(),
  loadFrmPalletBarcodeDetailsSaga(),
  loadFrmPalletOpsFormSaga(),
  delFrmPalletBarcodeSaga(),
  finishFrmPalletOpsSaga(),
  initLoadFrmPalletOpsForm(),
  loadFrmPalletOrderLine(),
  pushFrmPallet2ServerSaga()
];
